package com.xxx.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxx.common.IdWorkerManage;
import com.xxx.common.dto.Page;
import com.xxx.common.dto.Pagination;
import com.xxx.dao.DossierDao;
import com.xxx.model.Dossier;
import com.xxx.model.FamilyHistory;
import com.xxx.service.DossierService;
import com.xxx.service.FamilyHistoryService;

@Service
public class DossierServiceImpl implements DossierService {
	
	@Autowired
	private DossierDao dossierDao;
	
	@Autowired
	private FamilyHistoryService familyHistoryService;
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public long saveOrUpdate(Dossier dossier) throws Exception {
		if(dossier.getId() == null){
			dossier.setId(IdWorkerManage.getId());
			return dossierDao.save(dossier);
		}
		return dossierDao.update(dossier);
	}
	
	@Override
	@Transactional
	public long save(Dossier dossier, List<String> familyHistoryList) throws Exception {
		long id = this.saveOrUpdate(dossier);
		if(id > 0){
			List<FamilyHistory> list = new ArrayList<>();
			FamilyHistory familyHistory = null;
			JSONObject parseObject = null;
			familyHistoryService.deleteByDossierId(id);
			for(String string : familyHistoryList){
				parseObject = JSON.parseObject(string);
				String relatives = parseObject.getString("relatives");
				int age = parseObject.getInteger("age");
				String medicalhistory = parseObject.getString("medicalhistory");
				familyHistory = new FamilyHistory(IdWorkerManage.getId(), id, relatives, age, medicalhistory);
				list.add(familyHistory);
			}
			if (!list.isEmpty()) {
				if(familyHistoryService.batchSave(list) <= 0){
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //事物回滚
				}
			}
			return id;
		}
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //事物回滚
		return 0;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean delete(long id) throws Exception{
		familyHistoryService.deleteByDossierId(id);
		return dossierDao.delete(id);
	}

	@Override
	public Dossier getEntity(long id) throws Exception{
		return dossierDao.getEntity(id);
	}

	@Override
	public int getCount(Map<String, Object> whereMap) {
		return dossierDao.getCount(whereMap);
	}
	
	@Override
	public List<Dossier> getList(Map<String, Object> whereMap) {
		return dossierDao.getList(whereMap);
	}

	@Override
	public List<Dossier> getList(Map<String, Object> whereMap, Pagination pagination) {
		return dossierDao.getList(whereMap, pagination);
	}
	
	@Override
	public Page<Dossier> getPage(Map<String, Object> whereMap, Pagination pagination) {
		int count = this.getCount(whereMap);
		if(count > 0){
			List<Dossier> list = this.getList(whereMap, pagination);
			return new Page<Dossier>(pagination.getCurrentPage(), pagination.getPageSize(), count, list);
		}
		return new Page<Dossier>(pagination.getCurrentPage(), count);
	}
}
